﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class fckeditor_editor_filemanager_connectors_aspx_upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        HttpPostedFile pfile = null;
        string apppath = this.Request.ApplicationPath == "/" ? "/" : this.Request.ApplicationPath + "/";
        string pfilename = "";
        string pfileextension = "";
        string filesavepath = "";
        string filereturnpath = "";
        if (this.Request.Files[0] == null)
            return;
        if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["FCKUploadPath"]))
            return;

        pfile = this.Request.Files[0];
        pfilename = System.Guid.NewGuid().ToString();
        pfileextension = pfile.FileName.Substring(pfile.FileName.LastIndexOf('.') - 1).Trim();

        filesavepath = this.Server.MapPath(apppath + ConfigurationManager.AppSettings["FCKUploadPath"]) + "\\" + pfilename + pfileextension;
        filereturnpath = apppath + ConfigurationManager.AppSettings["FCKUploadPath"] + "/" + pfilename + pfileextension;

        //System.IO.File.WriteAllText("c:\\1.txt", filesavepath);
        pfile.SaveAs(filesavepath);


        //Response.Write("<script type=\"text/javascript\">");

        //// Minified version of the document.domain automatic fix script.
        //// The original script can be found at _dev/domain_fix_template.js
        //Response.Write(@"(function(){var d=document.domain;while (true){try{var A=window.top.opener.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();");

        ////if (isQuickUpload)
        //Response.Write(String.Format("window.parent.OnUploadCompleted(0,'{0}','{1}') ", filereturnpath, pfilename + pfileextension));
        ////else
        ////    Response.Write("window.parent.frames['frmUpload'].OnUploadCompleted(" + errorNumber + ",'" + fileName.Replace("'", "\\'") + "') ;");

        //Response.Write("</script>");
        SendResults(0, filereturnpath, pfilename + pfileextension);



    }

    private void SendResults(int errorNumber)
    {
        SendResults(errorNumber, "", "", "");
    }

    private void SendResults(int errorNumber, string fileUrl, string fileName)
    {
        SendResults(errorNumber, fileUrl, fileName, "");
    }

    private void SendResults(int errorNumber, string fileUrl, string fileName, string customMsg)
    {
        Response.Clear();

        Response.Write("<script type=\"text/javascript\">");
        Response.Write("window.parent.OnUploadCompleted(" + errorNumber + ",'" + fileUrl.Replace("'", "\\'") + "','" + fileName.Replace("'", "\\'") + "','" + customMsg.Replace("'", "\\'") + "') ;");
        Response.Write("</script>");

        Response.End();
    }
}
